#include "ad_userinfo.h"
#include "ui_ad_userinfo.h"

#include <QMessageBox>

AD_UserInfo::AD_UserInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AD_UserInfo)
{
    ui->setupUi(this);
}

AD_UserInfo::~AD_UserInfo()
{
    delete ui;
}

DB_userInfo AD_UserInfo::getData()
{
    DB_userInfo rData;
    rData.account = ui->lineEdit->text().toInt();
    rData.pwd = ui->lineEdit_2->text();
    rData.name = ui->lineEdit_3->text();

    rData.isAdmin = ui->checkBox->isChecked()?1:0;
    rData.isTeacher = ui->checkBox_2->isChecked()?1:0;
    rData.isCounselor = ui->checkBox_3->isChecked()?1:0;

    return rData;
}

void AD_UserInfo::setData(DB_userInfo data)
{
    ui->lineEdit->setText(QString::number(data.account));
    ui->lineEdit_2->setText(data.pwd);
    ui->lineEdit_3->setText(data.name);

    ui->checkBox->setChecked(data.isAdmin);
    ui->checkBox_2->setChecked(data.isTeacher);
    ui->checkBox_3->setChecked(data.isCounselor);

}

void AD_UserInfo::setDataByAccount(int account)
{
    auto user = DB->getAllUserInfo(QString ("account = %1").arg(account));

    if(user.count() >0){
        setData(user.at(0));
    }
}

void AD_UserInfo::on_pushButton_clicked()
{
    ui->lineEdit->setReadOnly(false);

    auto newUser = getData();
    auto user = DB->getAllUserInfo(QString ("account = %1").arg(newUser.account));

    if(user.count() >0){
        auto btn = QMessageBox::question(nullptr,"已经存在相同账号","是否确定更新该用户");
        if(btn == QMessageBox::Yes){
            newUser.updateOne();
        }
    }else{
        if(newUser.insertOne()){
            LOG("添加成功");
        }else{
            LOG("添加失败");
        }
    }


    PAGEJUMP->setPage(DK_AD_userMgrList);
}


void AD_UserInfo::on_pushButton_2_clicked()
{
    PAGEJUMP->setPage(DK_AD_userMgrList);
}

